//
//  BIDHistoryRemind.m
//  theFifth
//
//  Created by zd2011 on 13-6-8.
//
//

#import "BIDHistoryClock.h"
#define kListOfReminds @"list"
#import "BIDAClock.h"
#import "BIDClockVC.h"
#import "BIDClockManager.h"
@interface BIDHistoryClock ()
{
    NSArray *listReminds;
     UIBarButtonItem *addItem;
}
@end

@implementation BIDHistoryClock
@synthesize appIconNumber,isAppIconNumber;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   // [self.tableView reloadData];
   // NSLog(@"%d",isAppIconNumber);
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    if (isAppIconNumber) {
        return appIconNumber;
    }
    else
        
    return [[BIDClockManager shareManager] countOfClocks];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if (isAppIconNumber) {
            UILabel *timelabel=[[UILabel alloc] init];
            timelabel.frame=CGRectMake(10, 10, 300, 30);
            timelabel.tag=10;
            [cell addSubview:timelabel];
            [timelabel release];
            UILabel *otherLabel=[[UILabel alloc] init];
            otherLabel.frame=CGRectMake(10, 50, 300, 25);
            otherLabel.tag=11;
            [cell addSubview:otherLabel];
            [otherLabel release];
        }
    }
    BIDAClock *remind;
    if (isAppIconNumber) {
        
        remind=[[[BIDClockManager shareManager] listOfUsedClocks:appIconNumber] objectAtIndex:indexPath.row];
        UILabel *timeLa=(UILabel*)[cell viewWithTag:10];
        timeLa.text=remind.time;
        UILabel *otherL=(UILabel*)[cell viewWithTag:11];
        otherL.text=remind.other;
        NSLog(@"%@",remind.time);
        
    }
    else{
    remind=[[[BIDClockManager shareManager] listOfClocks] objectAtIndex:indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
    cell.textLabel.text=[NSString stringWithFormat:@"%@--%@",remind.time,remind.other];
    }
    
   
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (isAppIconNumber) {
        return [NSString stringWithFormat:@"当前闹钟:"];
    }
    else

    return [NSString stringWithFormat:@"闹钟:"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isAppIconNumber) {
        return 80;
    }
    else
        return 44;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    BIDAClock *remind=[[[BIDClockManager shareManager] listOfClocks] objectAtIndex:indexPath.row];
    BIDClockVC *nect=[[BIDClockVC alloc] initWithNibName:@"BIDClockVC" bundle:nil];
    nect.aClock=remind;
    [self.navigationController pushViewController:nect animated:YES];
    [nect release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
  
}
-(void)viewWillAppear:(BOOL)animated
{
    if (isAppIconNumber) {
        self.title=@"当前闹钟";
    }
    else
    {   self.title=@"闹钟";}
    self.navigationController.navigationBarHidden=NO;
    [self.tableView reloadData];

    [self initNavView];
}
-(void)initNavView
{
    addItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addARemindInfo:)];
    self.navigationItem.rightBarButtonItem=addItem;
    
}
-(void)addARemindInfo:(UIButton*)send
{
    BIDClockVC *next=[[BIDClockVC alloc] init];
    [self.navigationController pushViewController:next animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
    if (isAppIconNumber) {
        [self alterViewPush];
    }
    isAppIconNumber=NO;
}
-(void)alterViewPush
{
    UIAlertView *alter=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"删除" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    // alter.alertViewStyle
    [alter show];
    [alter release];
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex==1) {
        [[BIDClockManager shareManager] deleteCurrentClocks];
        [self.tableView reloadData];
    }
    
}
@end
